package com.jwdf.uda.ui.activity.presenter;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.ExtraBaseQuestion;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.FollowStatusEvent;
import com.jwdf.uda.event.QuizDetailEvent;
import com.jwdf.uda.event.ReplyDetailEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.QuizReplyDelCommentAction;
import com.jwdf.uda.http.action.QuizReplyGetLikesAction;
import com.jwdf.uda.http.action.QuizReplyLikeAction;
import com.jwdf.uda.http.action.ReplyDelAction;
import com.jwdf.uda.http.action.ReplyDetailAction;
import com.jwdf.uda.http.action.ReplyGetCommentListAction;
import com.jwdf.uda.http.action.UserAddFollowAction;
import com.jwdf.uda.http.action.UserCancelFollowAction;
import com.jwdf.uda.ui.activity.QuizDetailActivity;
import com.jwdf.uda.ui.activity.ReplyCommentAddActivity;
import com.jwdf.uda.ui.activity.ReplyDetaiActivtiy;
import com.jwdf.uda.ui.activity.view.IReplyDetailView;
import com.jwdf.uda.utils.ClipUtils;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ShareUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.utils.ToastUtils;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/18.
 */
public class ReplyDetailPresenter extends APresenter<IReplyDetailView> {
    public PbBean.PbResQuestionFindAnswer mDetail;
    private int mReplyId;
    private boolean mIsLike = false;

    public ReplyDetailPresenter(IReplyDetailView iView, int reply) {
        super(iView);
        mReplyId = reply;
    }
    public void toQuizReplyCommentAddActivity() {
        ReplyCommentAddActivity.startActivity(mIView.getFragment().getActivity(), mReplyId);
    }
    public void toQuizReplyCommentAddActivity(int atCommentId, String atNickname) {
        ReplyCommentAddActivity.startActivity(mIView.getFragment().getActivity(), mReplyId, atCommentId, atNickname);
    }
    public void doCopyContentAction(String content) {
        ClipUtils.copy(mIView.getFragment().getActivity(), StringUtils.filterNullAndTrim(content));
        ToastUtils.show(mIView.getFragment().getContext(), R.string.toast_copy_success);
    }
    public void showDelCommentDialog(int commentId) {
        mIView.showDelCommentDialog(commentId);
    }
    public void doDelCommentAction(int commentId, final PbBean.PbSignNavType type) {
        mIView.getFragment().showProgressDialog();
        QuizReplyDelCommentAction action = QuizReplyDelCommentAction.newInstance(mIView.getFragment().getActivity(), commentId);
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResCommentDelComment>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResCommentDelComment> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(mIView.getFragment().getView() != null) {
                    mIView.getFragment().closeProgressDialog();
                    if(HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doDelCommentSuccess(type);
                }
            }
        }));
    }
    private void doDelCommentSuccess(PbBean.PbSignNavType type) {
        getCommentListAction(0, type);
    }
    public void getCommentListAction(final int offset, PbBean.PbSignNavType type) {
        if(offset == 0) {
            mIView.getFragment().showProgressDialog();
        }
        ReplyGetCommentListAction action = ReplyGetCommentListAction.newInstance(mIView.getFragment().getActivity(), mReplyId, offset, type);
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResCommentGetCommentList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResCommentGetCommentList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(mIView.getFragment().getView() != null) {
                    mIView.getFragment().closeProgressDialog();
                    if(HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doLoadCommentListSuccess(offset, response.body());
                }
            }
        }));
    }
    private void doLoadCommentListSuccess(int offset, PbBean.PbResCommentGetCommentList comments) {
        //        if(offset > 0) {
        //            mIView.loadMoreComments(comments.getCommentListList());
        //        } else {
        //            mIView.refreshComments(comments.getCommentListList(), comments.getCommentCount());
        //            //            EventBusUtil.post(GuideTabCountEvent.newInstanceForComment(mReplyId, comments.getCommentListCount()));
        //        }
        setPullLoadEnable(comments.getCommentListCount() == ((ReplyDetaiActivtiy.ReplyDetailFragment) mIView.getFragment()).getPageLength());
    }
    public void setPullLoadEnable(boolean enable) {
        mIView.setPullLoadEnable(enable);
    }
    public void doPraiseAction(final View v) {
//        mIView.getFragment().showProgressDialog();
        QuizReplyLikeAction action = QuizReplyLikeAction.newInstance(mIView.getFragment().getActivity(), mReplyId);
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResQuestionAddAnswerLike>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResQuestionAddAnswerLike> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(mIView.getFragment().getView() != null) {
//                    mIView.getFragment().closeProgressDialog();
                    if(HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    EventBusUtil.post(new ReplyDetailEvent(ReplyDetailEvent.EVENT_DO_REPLY_PRAISE));
                    doLikeResponse();

                    ScaleAnimation animation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(300);//设置动画持续时间
                    v.startAnimation(animation);
                }
            }
        }));
    }
    private void doLikeResponse() {
        mIsLike = !mIsLike;
        mIView.showPraiseView(mIsLike);
        requestDetail();
    }
    public void requestDetail() {
        if(mIView.getAdapterCount() == 0) {
            mIView.getFragment().showProgressDialog();
        }
        ReplyDetailAction action = ReplyDetailAction.newInstance(mIView.getFragment().getActivity(), mReplyId);
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResQuestionFindAnswer>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResQuestionFindAnswer> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(mIView.getFragment().getView() != null) {
                    mIView.getFragment().closeProgressDialog();
                    if(HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    final PbBean.PbResQuestionFindAnswer body = response.body();
                    doSuccessResponse(body);
                }
            }
        }));
    }
    private void doSuccessResponse(PbBean.PbResQuestionFindAnswer body) {
        this.mDetail = body;
        if(mDetail == null)
            return;

        mIView.showDetailData(mDetail);
        setPullLoadEnable(false);
        //        if(mIView.getCurrentTab() == ReplyDetailItem.ReplyTab.COMMENT) {
        //            setPullLoadEnable(mDetail.getCommentListCount() == ((ReplyDetaiActivtiy.ReplyDetailFragment) mIView.getFragment()).getPageLength());
        //        } else {
        //        }
//        getLikeListAction();
        mIsLike = mDetail.getAnswerInfo().getIsLiker() == 1;
        mIView.showPraiseView(mIsLike);
        mIView.showBottomView(mDetail.getAnswerInfo().getCommentCount(), mDetail.getAnswerInfo().getLikeCount());
    }
    public void getLikeListAction() {
        QuizReplyGetLikesAction action = QuizReplyGetLikesAction.newInstance(mIView.getFragment().getActivity(), mReplyId, 0);
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResQuestionAnswerLikeList>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResQuestionAnswerLikeList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(mIView.getFragment().getView() != null) {
                    if(HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doLoadLikeListSuccess(response.body());
                }
            }
        }));
    }
    private void doLoadLikeListSuccess(PbBean.PbResQuestionAnswerLikeList likes) {
        mIView.refreshLikes(likes.getLikerInfoList(), likes.getLikeCount());
        //            EventBusUtil.post(GuideTabCountEvent.newInstanceForComment(mReplyId, comments.getCommentListCount()));
    }
    public void doDelReplyAction() {
        ReplyDelAction action = ReplyDelAction.newInstance(mIView.getFragment().getActivity(), mReplyId);
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResQuestionDelAnswer>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResQuestionDelAnswer> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(mIView.getFragment().getView() != null) {
//                    mIView.getFragment().closeProgressDialog();
                    if(HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    mIView.getFragment().getActivity().finish();
                    EventBusUtil.post(new QuizDetailEvent(QuizDetailEvent.EVENT_QUIZ_REPLY_DEL));
                }
            }
        }));
    }

    public void doShareAction(Context context) {
//        if(mDetail != null && !TextUtils.isEmpty(mDetail.getAnswerInfo().getShareUrl())) {
        String title="【问题】"+mDetail.getQuestionTitle()+"-"+mDetail.getAnswerInfo().getUserInfo().getNickname()+"的回答";
        if(mDetail.getLikeCount()>0){
            title=title+","+mDetail.getLikeCount()+"个赞同";
        }
            ShareUtil.onekeyShare(context, mDetail.getAnswerInfo().getQuickDescription(), mDetail.getAnswerInfo().getShareUrl(),title);
//        }
    }

    public void onEventMainThread(ReplyDetailEvent event) {
        switch(event.getEvent()) {
            //            case ReplyDetailEvent.EVENT_LIKE_COMMENT:
            //            case ReplyDetailEvent.EVENT_UNLIKE_COMMENT:
            //                doLikeAction(event);
            //                break;
            case ReplyDetailEvent.EVENT_TAB_COMMENT:
                //                mIView.setCurrentTab(ReplyDetailItem.ReplyTab.COMMENT);
                setPullLoadEnable(event.count == ((ReplyDetaiActivtiy.ReplyDetailFragment) mIView.getFragment()).getPageLength());
                break;
            case ReplyDetailEvent.EVENT_TAB_LIKE:
                //                mIView.setCurrentTab(ReplyDetailItem.ReplyTab.LIKE);
                setPullLoadEnable(event.count == ((ReplyDetaiActivtiy.ReplyDetailFragment) mIView.getFragment()).getPageLength());
                break;
            case ReplyDetailEvent.EVENT_REFRESH_LIKE:
                getLikeListAction();
                break;
            case ReplyDetailEvent.EVENT_COMMENT_ADD:
                requestDetail();
                break;
            case ReplyDetailEvent.EVENT_TITLE_ACTION_FINISH:
                mIView.getFragment().getActivity().finish();
                break;
            case ReplyDetailEvent.EVENT_TITLE_ACTION_START:
                ExtraBaseQuestion baseQuestion = event.getExtraBaseQuestion();
                QuizDetailActivity.start(mIView.getFragment().getContext(), baseQuestion.getQuestionId(), baseQuestion.isAuthor());
                break;
            case ReplyDetailEvent.EVENT_USER_ADD_FOLLOW:
                addFollow(event);
                break;
            case ReplyDetailEvent.EVENT_USER_CANCEL_FOLLOW:
                cancelFollow(event);
                break;
        }
    }
    private void addFollow(final ReplyDetailEvent event) {
//        mIView.getFragment().showProgressDialog();
        mIView.getFragment().onNewRequestCall(UserAddFollowAction.newInstance(mIView.getFragment().getActivity(), event.getUserId()).request(new AHttpService.IResCallback<PbBean.PbResUserAddFollow>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserAddFollow> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(mIView.getFragment().getView() != null) {
//                    mIView.getFragment().closeProgressDialog();
                    if(HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_net)) {
                        return;
                    }
                    doFollowSuccess(event);
                }
            }
        }));
    }
    private void doFollowSuccess(ReplyDetailEvent e) {
        FollowStatusEvent event = new FollowStatusEvent();
        event.isFollowed = ReplyDetailEvent.EVENT_USER_ADD_FOLLOW == e.getEvent();
        EventBusUtil.post(event);
    }
    private void cancelFollow(final ReplyDetailEvent event) {
//        mIView.getFragment().showProgressDialog();
        mIView.getFragment().onNewRequestCall(UserCancelFollowAction.newInstance(mIView.getFragment().getActivity(), event.getUserId()).request(new AHttpService.IResCallback<PbBean.PbResUserDelFollow>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserDelFollow> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(mIView.getFragment().getView() != null) {
//                    mIView.getFragment().closeProgressDialog();
                    if(HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_net)) {
                        return;
                    }
                    doFollowSuccess(event);
                }
            }
        }));
    }

    //    public void doLikeAction(final ReplyDetailEvent event) {
    //        mIView.getFragment().showProgressDialog();
    //        ReplyLikeCommentAction action = ReplyLikeCommentAction.newInstance(mIView.getFragment().getActivity(), event.item.commentId);
    //        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResCommentLikeComment>() {
    //            @Override
    //            public void onCallback(int resultCode, Response<PbBean.PbResCommentLikeComment> response, PbBean.PbBaseServerInfo baseServerInfo) {
    //                if (mIView.getFragment().getView() != null) {
    //                    mIView.getFragment().closeProgressDialog();
    //                    if (HttpHelper.showError(mIView.getFragment().getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
    //                        return;
    //                    }
    //                    doLikeSuccess(event);
    //                }
    //            }
    //        }));
    //    }
    //
    //    private void doLikeSuccess(ReplyDetailEvent e) {
    //        ReplyDetailItem detailItem = e.item;
    //        detailItem.isLikeComment = ReplyDetailEvent.EVENT_LIKE_COMMENT == e.getEvent();
    //        detailItem.commentLikeCount = ReplyDetailEvent.EVENT_LIKE_COMMENT == e.getEvent() ? e.item.commentLikeCount + 1 : e.item.commentLikeCount - 1;
    //        ReplyDetailItemClickEvent event = new ReplyDetailItemClickEvent(detailItem, ReplyDetailItemClickEvent.EVENT_COMMENT_LIKE_CLICK);
    //        EventBusUtil.post(event);
    //    }

    //    public void onEventMainThread(ReplyDetailItemClickEvent event) {
    //        ReplyDetailItem item = event.getItem();
    //        switch(item.itemType) {
    //            case ReplyDetailItem.TYPE_COMMENT_TXT:
    //            case ReplyDetailItem.TYPE_COMMENT_IMAGE:
    //                if(ReplyDetailItemClickEvent.EVENT_COMMENT_ITEM_CLICK == event.getEvent()) {
    //                    showCommentChoiceDialog(item.comment);
    //                }
    //                break;
    //        }
    //    }
    public void showCommentChoiceDialog(PbBean.PbBaseComment comment) {
        PbBean.PbUserSimple userInfo = comment.getUserInfo();
        if(userInfo.getUserId() != null && userInfo.getUserId().equals(ConfigUtils.getUserId(mIView.getFragment().getContext()))) {
            mIView.showCommentChoiceDialog(comment, true);
        } else {
            mIView.showCommentChoiceDialog(comment, false);
        }
    }

}
